# -*- coding: utf8 -*-

from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm
from django import forms

class MyProfile(models.Model):
	user = models.ForeignKey(User, unique=True);

	student = models.BooleanField(default = True);

class Image(models.Model):
	menu = models.ForeignKey('Menu')

	def __unicode__(self):
		return self.menu.name

class Recommendation(models.Model):
    user = models.ForeignKey(User);
    menu = models.ForeignKey('Menu');
    value = models.PositiveIntegerField();

    def __unicode__(self):
        return self.user.username + '_' + self.menu.name + '_' + str(self.value);


class MenuCategory(models.Model):
	name = models.CharField(max_length = 100);
	
	shop = models.ForeignKey('Shop');

	def __unicode__(self):
		return self.name + ' - ' + self.shop.name;

class Menu(models.Model):
	name = models.CharField(max_length = 100);
	price = models.PositiveIntegerField();
	desc = models.TextField(blank=True, null=True);
	avail = models.BooleanField(default = True);

	shop = models.ForeignKey('Shop');
	category = models.ForeignKey('MenuCategory');
	tags = models.ManyToManyField('Tag', blank=True);

	def __unicode__(self):
		return self.name + ' - ' + self.category.name + ' - ' + self.shop.name;

class Shop(models.Model):
	name = models.CharField(max_length = 100);
	phone = models.CharField(max_length = 20);
	desc = models.TextField(blank=True, null=True);

	tags = models.ManyToManyField('Tag', blank=True);
	category = models.ManyToManyField('ShopCategory', blank=True);

	def __unicode__(self):
		return self.name;

class ShopCategory(models.Model):
	name = models.CharField(max_length = 50);

	def __unicode__(self):
		return self.name;

class ShopComment(models.Model):
	content = models.TextField();
	time = models.DateTimeField(auto_now_add = True);
	ip = models.IPAddressField();
   
	user = models.ForeignKey(User);
	shop = models.ForeignKey('Shop');

	def __unicode__(self):
		return self.content
	def to_dict(self):
		return {'user':self.user.username,'shop':self.shop.id,'content':self.content,'time':self.time.ctime(),'ip':self.ip}
        

class MenuComment(models.Model):
	content = models.TextField();
	time = models.DateTimeField(auto_now_add = True);
	ip = models.IPAddressField();

	user = models.ForeignKey(User);
	menu = models.ForeignKey('Menu');

	def __unicode__(self):
		return self.content;

class Tag(models.Model):
	content = models.CharField(max_length=30);

	def __unicode__(self):
		return self.content;


class Order(models.Model):
	price = models.PositiveIntegerField();
	time = models.DateTimeField(auto_now_add = True);

	user = models.ForeignKey(User);
	menu = models.ManyToManyField('Menu');

	def __unicode__(self):
		return u"주문 " + str(self.id);

class Report(models.Model):
	content = models.TextField();
	category = models.SlugField(blank=True);

	user = models.ForeignKey(User);

	def __unicode__(self):
		return self.content;

####################ModelForm##############################
	
class MenuForm(ModelForm):
	class Meta:
		model = Menu
		fields = ['name','price','desc','img','shop'];

class ShopCommentForm(ModelForm):
    class Meta:
        model = ShopComment

class OrderForm(ModelForm):
	class Meta:
		model = Order
		fields = ['user','menu','price']

class CustomUserForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(CustomUserForm, self).__init__(*args, **kwargs)
        self.fields['password'].widget = forms.PasswordInput()
    class Meta:
        model = User

class CustomShopCommentForm(ModelForm):
    class Meta:
        model = ShopComment
        exclude = ('ip',)